package cn.lh.org.service.impl;

import cn.lh.basic.service.impl.BaseServiceImpl;
import cn.lh.basic.util.BaiduAuditUtils;
import cn.lh.org.domain.Employee;
import cn.lh.org.domain.Shop;
import cn.lh.org.domain.ShopAuditLog;
import cn.lh.org.dto.ShopDto;
import cn.lh.org.exception.BusinessException;
import cn.lh.org.mapper.EmployeeMapper;
import cn.lh.org.mapper.ShopAuditLogMapper;
import cn.lh.org.mapper.ShopMapper;
import cn.lh.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.List;

@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private JavaMailSender javaMailSender;

    @Override
    @Transactional
    public void settlement(Shop shop) {
        Employee admin = shop.getAdmin();
        if (StringUtils.isEmpty(shop.getName())
                || StringUtils.isEmpty(shop.getTel()) || StringUtils.isEmpty(shop.getAddress()) || StringUtils.isEmpty(admin.getUsername()) || StringUtils.isEmpty(admin.getPhone())
                || StringUtils.isEmpty(admin.getEmail()) || StringUtils.isEmpty(admin.getPassword()) || StringUtils.isEmpty(admin.getComfirmPassword())
        ) {
            throw new BusinessException("参数不能为空!");
        }
        if (!admin.getPassword().equals(admin.getComfirmPassword())) {
            throw new BusinessException("两次密码不一致!");
        }
        Shop shopTemp = shopMapper.findByNameAndTel(shop);
        if (shopTemp != null) {
            throw new BusinessException("店铺已经入驻，请直接登陆！忘记密码请找回密码!");
        }
        if (BaiduAuditUtils.textCensor(shop.getName())) {
            throw new BusinessException("店铺名称违规");
        }
        employeeMapper.save(admin);
        shop.setAdmin_id(admin.getId());
        shopMapper.save(shop);
        admin.setShop_id(shop.getId());
        employeeMapper.update(admin);
    }

    @Override
    public void pass(ShopAuditLog shopAuditLog) throws MessagingException {
        Shop shop = shopMapper.loadById(shopAuditLog.getShop_id());
        shop.setState(2);
        shopMapper.update(shop);
        shopAuditLog.setState(2);
        shopAuditLog.setAudit_id(1L);
        shopAuditLogMapper.save(shopAuditLog);

        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("2494733719@qq.com");
        helper.setSubject("店铺激活邮件");
        helper.setText("<h1>你的店铺已经注册!!!</h1><img src='http://123.207.27.208/group1/M00/00/FF/CgAIC2LeCxSARrn_AAE8pnDRIMI575.jpg' ><a href='http://localhost:8080/shop/active/22'>点击该链接激活</a>",true);
        //收件人
        Employee employee = employeeMapper.loadById(shop.getAdmin_id());
        String email = employee.getEmail();
        helper.setTo(email);
        javaMailSender.send(mimeMessage);
    }

    @Override
    public void reject(ShopAuditLog shopAuditLog) throws MessagingException {
        Shop shop = shopMapper.loadById(shopAuditLog.getShop_id());
        shop.setState(4);
        shopMapper.update(shop);
        shopAuditLog.setState(4);
        shopAuditLog.setAudit_id(1L);
        shopAuditLogMapper.save(shopAuditLog);

        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("2494733719@qq.com");
        helper.setSubject("店铺激活邮件");
        helper.setText("<h1>你的店铺被驳回!!!</h1><a href='http://localhost:8080/shop/active/22'>点击该链接申诉</a>",true);
        //收件人
        Employee employee = employeeMapper.loadById(shop.getAdmin_id());
        String email = employee.getEmail();
        helper.setTo(email);
        javaMailSender.send(mimeMessage);
    }

    @Override
    public List<ShopDto> getCountByState() {
        return shopMapper.getCountByState();
    }

}
